/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package etc;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.FileReader;
import java.io.IOException;
import javax.swing.JOptionPane;

/**
 *
 * @author Zoli
 */
public class DocOperations
{
    
    private File file;

    public DocOperations(String s)
    {
        this.file=new File(s);
    }

    public String readDocument()
    {
        String documentText="";
		try
        {
            BufferedReader inputstream = new BufferedReader(new FileReader(this.file));
            String temp;

            while ((temp=inputstream.readLine())!= null)
            {
                documentText=documentText + temp + "\n";
            }
            inputstream.close();
        }
        catch (FileNotFoundException ex)
        {
            JOptionPane.showMessageDialog(null, "Could not open file!\n" + ex.getMessage(),"File open error",JOptionPane.ERROR_MESSAGE);
        }
        catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, "Could not open file!\n" + ex.getMessage(),"File open error",JOptionPane.ERROR_MESSAGE);
        }
		// TODO Auto-generated method stub
		return documentText;
    }

    public void writeDocument(String s)
    {
        try
        {
            if (!this.file.exists())
                this.file.createNewFile();

            FileOutputStream fop=new FileOutputStream(this.file);

            fop.write(s.getBytes());
            fop.flush();
            fop.close();
        }
        catch (IOException ex)
        {
            JOptionPane.showMessageDialog(null, ex.getMessage(),"Error",JOptionPane.ERROR_MESSAGE);
        }

    }

}
